package com.yixq.soa.service.comm;

import java.io.Serializable;
/**
 * 服务端返回数据的包装类
 * @ClassName: data
 * @Description: 
 * @author yixq yixq@doordu.com 
 * @date 2017年4月20日 上午9:42:37
 * @version V1.0  
 * @param <T>
 */
public class ResultData<T extends Object> implements Serializable {
	private static final long serialVersionUID = -7996028449496083246L;

	private String code;

	private String desc;// 响应结果描述

	private String msg;// 响应结果描述

	private Exception exception;// 异常信息

	private T data;// 响应结果集

	public ResultData() {
	}

	public ResultData(String code) {
		super();
		this.code = code;
	}

	public ResultData(String code, T data) {
		super();
		this.code = code;
		this.data = data;
	}

	public ResultData(String code, String desc, Exception exception, T data) {
		super();
		this.code = code;
		this.desc = desc;
		this.exception = exception;
		this.data = data;
	}

    public static <T> ResultData<T> okResult(T data) {
        ResultData<T> resultData = new ResultData<T>(ResultCode.RESPONSE_SUCCESS);
        resultData.setData(data);
        return resultData;
    }

	public static <T> ResultData<T> defaultOk() {
		return new ResultData(ResultCode.RESPONSE_SUCCESS);
	}

    public boolean success() {
        return this.code.equals(ResultCode.RESPONSE_SUCCESS);
    }

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public Exception getException() {
		return exception;
	}

	public void setException(Exception exception) {
		if (null != exception)
			this.exception = new RuntimeException("-- 服务方异常:" + exception.toString());
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	/**
	 * 重载方法
	 * 
	 * @return
	 */
	@Override
	public String toString() {
		return "data [code=" + code + ", msg=" + msg + ", desc=" + desc + ", exception=" + exception
				+ ", data=" + data + "]";
	}
}
